﻿package cn.poco.pepsi.activity.guest;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.poco.pepsi.R;
import cn.poco.pepsi.adapter.FoodGuestListAdapter;
import cn.poco.pepsi.bean.Guest;
import cn.poco.pepsi.service.impl.GuestImpl;
import cn.poco.pepsi.util.Cons;
import cn.poco.pepsi.util.GpsLocationUtil;

/**
 * 同城食客列表
 * @author tanglx
 * @同城食客里面GPS数据是只要程序在任何一个地方有过定位则取相应的定位数据
 */
public class FoodGuestListActivity extends Activity {
	public static final String TAG = "FoodGuestListActivity";
	//tab cho0se
	private int type;
	private Handler tHandler;
	List<Guest> list, listRe;
	private ListView listView;
	private FoodGuestListAdapter fAdapter;
	private GpsLocationUtil nGpsLocationUtil;
	private LinearLayout preLayout;

	// 更多
	private LinearLayout viewMore;
	private ProgressBar listFooterProgressbar;
	private TextView listFooterTextView;
	// 错误处理
	private LinearLayout error;
	private Button tautology;
	private Button setBtn;
	// 无数据处理
	private TextView noDataTextView;

	// 条件
	private int l = 10;
	private int s = 0;
	private String range = "0.5";
	private String lat, lon;
	private String cityCode;
	
	private String myId;
	private String cicly="60";
	
	
	
	/**
	 * isFirst仅仅用来确定是否是第一次进入，
	 * 以确定listView只加载一次FooterView
	 */
	private boolean isFirst = true;
	
	/**
	 * isLastData是不是最后一组数据，
	 * 以确实是否移除FooterView
	 */
	private boolean isLastData = false;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Cons.GPS_LOCATION_CHANGE:
				Bundle bundle = msg.getData();
				lat = String.valueOf(bundle.getDouble(Cons.GPS_LAT, 1));
				lon = String.valueOf(bundle.getDouble(Cons.GPS_LONG, 1));
				nGpsLocationUtil.unRequestListener();
				handler.sendEmptyMessage(1);
				break;
			case 1:
				//参数准备完毕 开始加载数据
				tHandler.post(runnable);
				break;
			case 2:
				// 开始加载第一次成功
				listView.setVisibility(View.VISIBLE);
				preLayout.setVisibility(View.GONE);
				error.setVisibility(View.GONE);
				noDataTextView.setVisibility(View.GONE);
				fAdapter= new FoodGuestListAdapter(list, FoodGuestListActivity.this, listView);
				fAdapter.setAdapter(fAdapter);
				//如果是第一次加载并且不是最后一组数据
				if (isFirst && isLastData==false) {
					isFirst=false;
					listFooterProgressbar.setVisibility(View.GONE);
					listFooterTextView.setText("更多");
					listView.addFooterView(viewMore);
				}
				listView.setAdapter(fAdapter);
				break;
			case 3:
				//无数据
				listView.setVisibility(View.GONE);
				preLayout.setVisibility(View.GONE);
				error.setVisibility(View.GONE);
				noDataTextView.setVisibility(View.VISIBLE);
				break;
			case 4:
				// 更多更新
				if (isLastData) {
					listView.removeFooterView(viewMore);
				}else {
					listFooterProgressbar.setVisibility(View.GONE);
					listFooterTextView.setVisibility(View.VISIBLE);
					listFooterTextView.setText("更多");
				}
				list.addAll(listRe);
				fAdapter.notifyDataSetChanged();
				break;
			case Cons.CONN_ERROR:
				listView.setVisibility(View.GONE);
				preLayout.setVisibility(View.GONE);
				error.setVisibility(View.VISIBLE);
				noDataTextView.setVisibility(View.GONE);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guest_list);
		initView();
		HandlerThread handlerThread = new HandlerThread(TAG);
		handlerThread.start();
		tHandler = new Handler(handlerThread.getLooper());

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 == listView.getCount() - 1&&isLastData==false) {
					s=s+10;
					listFooterProgressbar.setVisibility(View.VISIBLE);
					listFooterTextView.setText("加载");
					tHandler.post(runnableRe);
				}

			}
		});
		
		setBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
				startActivity(intent);
			}
		});

		tautology.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				handler.sendEmptyMessage(1);
			}
		});
	}

	protected void initView() {
		// 区分tab页
		type = getIntent().getExtras().getInt(Cons.IACTIVERANKLISTTYPE);
		myId=getIntent().getExtras().getString(Cons.MYID);
		listView = (ListView) findViewById(R.id.listview);
		preLayout = (LinearLayout) findViewById(R.id.progressbar);

		// 处理错误信息
		error = (LinearLayout) findViewById(R.id.error);
		error.setVisibility(View.GONE);
		tautology = (Button) findViewById(R.id.tautology);
		setBtn = (Button) findViewById(R.id.setnet);

		noDataTextView = (TextView) findViewById(R.id.ErrorMessage);

		SharedPreferences sharedPreferences = getSharedPreferences(Cons.GPS_CITY_CODE, MODE_PRIVATE);
		cityCode = sharedPreferences.getString(Cons.CITY_CODE, "000000");

		viewMore = (LinearLayout) LayoutInflater.from(FoodGuestListActivity.this).inflate(R.layout.list_footer, null);
		listFooterProgressbar = (ProgressBar) viewMore.findViewById(R.id.list_footer_progress);
		listFooterTextView = (TextView) viewMore.findViewById(R.id.list_footer_text);

		//是否已经定位
		if (Cons.GPS_LAT_DATA != null && Cons.GPS_LON_DATA != null) {
			lat = Cons.GPS_LAT_DATA;
			lon = Cons.GPS_LON_DATA;
			handler.sendEmptyMessage(1);
		} else {
			nGpsLocationUtil = GpsLocationUtil.getInstance(FoodGuestListActivity.this);
			nGpsLocationUtil.getLocation(handler);
		}
	}

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			try {
				if (type == 1) {
					list = new GuestImpl().getNearGuests(cityCode, lat, lon, range, s, l);
				}else if(type==2) {
					list=new GuestImpl().gethotGuests(cityCode, cicly,l , s);
				}else {
					list=new GuestImpl().getmyGuests(myId, s, l);
				}
				if (list != null) {
					int size = list.size();
					if (size<10) {
						isLastData = true;
					}
					if (size == 0) {
						//无数据
						handler.sendEmptyMessage(3);
					}else{
						//开始加载
						handler.sendEmptyMessage(2);
					}
					
				} else {
					handler.sendEmptyMessage(Cons.CONN_ERROR);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendEmptyMessage(Cons.CONN_ERROR);
			}
		}
	};

	Runnable runnableRe = new Runnable() {
		@Override
		public void run() {
			try {
				if (type == 1) {
					listRe = new GuestImpl().getNearGuests(cityCode, lat, lon, range, s, l);
				}else if(type==2) {
					listRe=new GuestImpl().gethotGuests(cityCode, cicly,s , l);
				}else {
					listRe=new GuestImpl().getmyGuests(myId, s, l);
				}
				
				if (listRe != null) {
					int size = listRe.size();
					if (size < 10) {
						isLastData = true;
					}
					handler.sendEmptyMessage(4);
				} else {
					handler.sendEmptyMessage(Cons.CONN_ERROR);
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendEmptyMessage(Cons.CONN_ERROR);
			}
		}
	};
}
